package com.jz.netthings.application.providerAllot.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jz.netthings.appengine.baseData.JzMachine;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.appengine.mapper.JzMachineMapper;
import com.jz.netthings.appengine.mapper.ProviderInfoMapper;
import com.jz.netthings.application.providerAllot.bean.ProviderAllot;
import com.jz.netthings.application.providerAllot.bean.QueryProviderAllot;
import com.jz.netthings.application.providerAllot.mapper.mysql.ProviderAllotMapper;
import com.jz.netthings.application.providerAllot.service.ProviderAllotService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.bean.system.JyCode;
import com.jz.netthings.bean.system.JyUser;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.system.mapper.mysql.JyCodeMapper;
import com.jz.netthings.system.mapper.mysql.JyUserMapper;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-03-13
 */
@Service
public class ProviderAllotServiceImpl extends ServiceImpl<ProviderAllotMapper, ProviderAllot> implements ProviderAllotService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
     JzMachineMapper jzMachineMapper;
     @Autowired
     ProviderInfoMapper providerInfoMapper;
     @Autowired
    JyCodeMapper jyCodeMapper;
    @Override
    public Page<ProviderAllot> selectProviderAllot(Page<ProviderAllot> page, QueryProviderAllot queryProviderAllot) throws AdscmSystemException {
        Wrapper<ProviderAllot> providerAllotWrapper =new EntityWrapper<>();

        boolean isCurrentDay = true;//是否只查询当天的，如果有其他条件的，则不是查询当天
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getCardId(),null)  &&
                AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getProviderName(),null)){
            Wrapper<ProviderInfo> providerInfoWrapper=new EntityWrapper<>();
            providerInfoWrapper.like("name",queryProviderAllot.getProviderName());
            providerInfoWrapper.eq("cardId",queryProviderAllot.getCardId());
            List<ProviderInfo> providerInfos = providerInfoMapper.selectList(providerInfoWrapper);
            if (providerInfos.size()>0){
                ProviderInfo providerInfo = providerInfos.get(0);
                isCurrentDay = false;
                providerAllotWrapper.eq("providerNo",providerInfo.getId());
            }else {
                throw new AdscmSystemException("查无此人");
            }
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getCardId(),null)  && !
                AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getProviderName(),null)){
            Wrapper<ProviderInfo> providerInfoWrapper=new EntityWrapper<>();
            providerInfoWrapper.eq("cardId",queryProviderAllot.getCardId());
            List<ProviderInfo> providerInfos = providerInfoMapper.selectList(providerInfoWrapper);
            if (providerInfos.size()>0){
                ProviderInfo providerInfo = providerInfos.get(0);
                isCurrentDay = false;
                providerAllotWrapper.eq("providerNo",providerInfo.getId());
            }else {
                throw new AdscmSystemException("查无此人");
            }
        }
        if (!AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getCardId(),null)  &&
                AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getProviderName(),null)){
            Wrapper<ProviderInfo> providerInfoWrapper=new EntityWrapper<>();
            providerInfoWrapper.like("name",queryProviderAllot.getProviderName());
            List<ProviderInfo> providerInfos = providerInfoMapper.selectList(providerInfoWrapper);
            if (providerInfos.size()>0){
                ProviderInfo providerInfo = providerInfos.get(0);
                isCurrentDay = false;
                providerAllotWrapper.eq("providerNo",providerInfo.getId());
            }else {
                throw new AdscmSystemException("查无此人");
            }
        }
       if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getRegisterNo(),null)){
           isCurrentDay = false;
           providerAllotWrapper.like("registerNo",queryProviderAllot.getRegisterNo());
       }
       if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getMachineNo(),null)){
           isCurrentDay = false;
            providerAllotWrapper.eq("machineNo",queryProviderAllot.getMachineNo());
        }

       if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getAllotType(),null)){
           isCurrentDay = false;
            providerAllotWrapper.eq("allotType",queryProviderAllot.getAllotType());
        }
       if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getStartAllotDate(),null)){
           isCurrentDay = false;
            providerAllotWrapper.ge("allotDate",queryProviderAllot.getStartAllotDate());
        }
       if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderAllot.getEndAllotDate(),null)){
           isCurrentDay = false;
            providerAllotWrapper.le("allotDate",queryProviderAllot.getEndAllotDate());
        }
        if (isCurrentDay){
            providerAllotWrapper.like("registerNo", AdscmDateUtils.getNowOnlyDateFomat2(new Date()));
        }
        providerAllotWrapper.orderBy("allotDate",false);
        Page<ProviderAllot> providerAllotPage = super.selectPage(page, providerAllotWrapper);
        List<ProviderAllot> providerAllots = providerAllotPage.getRecords();
        for (ProviderAllot providerAllot : providerAllots) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerAllot.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(providerAllot.getCreator());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    providerAllot.setCreatorName(jyUser.getName());
                }else {
                    providerAllot.setCreatorName(providerAllot.getCreator());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerAllot.getProviderNo(),null)){
                ProviderInfo providerInfo = providerInfoMapper.selectById(providerAllot.getProviderNo());
                if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo,null)){
                    providerAllot.setProviderNoName(providerInfo.getName());
                    providerAllot.setCardId(providerInfo.getCardId());
                }else {
                    providerAllot.setProviderNoName(providerAllot.getProviderNo());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerAllot.getMachineNo(),null)){
                JzMachine jzMachine = jzMachineMapper.selectById(providerAllot.getMachineNo());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jzMachine,null)){
                    providerAllot.setMachineNoName(jzMachine.getName());
                }else {
                    providerAllot.setMachineNoName(providerAllot.getMachineNo());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerAllot.getAllotType(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setType(15);
                jyCode.setCode(providerAllot.getAllotType());
                jyCode = jyCodeMapper.selectOne(jyCode);
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                    providerAllot.setAllotTypeName(jyCode.getName());
                }else {
                    providerAllot.setMachineNoName(providerAllot.getAllotType());
                }
            }

        }
        page.setTotal(super.selectCount(providerAllotWrapper));

        return page;
    }
}
